/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JDBC;
import Modele.Film;
import Modele.Planning;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author elfindel
 */
public class Main {

  private static Connexion conn=new Connexion();
 private static PreparedStatement ps = null;

    private static Connection c=null;
    private static ResultSet rs=null;

    private static PreparedStatement ps2=null;
    private static PreparedStatement ps3=null;
    private static PreparedStatement ps1=null;
    private static ResultSet rs0=null;

    private static PreparedStatement ps4=null;
    private static PreparedStatement ps5=null;
    private static ResultSet rs2=null;
    private static PreparedStatement ps6=null;
    private static ResultSet rs3=null;

    public Main() throws SQLException, Exception{
        try {
        c = conn.Connecter();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

public static void ajouter(Film f){
        try {
            c = conn.Connecter();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    System.out.println(f);
        try {
            ps1 = c.prepareStatement("INSERT INTO \"Films\" (\"numFilm\", \"numPlanning\", \"titre\", \"realisateur\", \"duree\", \"type\") VALUES (?, ?, ?, ?, ?, ?)");
            ps1.setInt(1, f.getNoFilm());
            ps1.setInt(2, 1);
            ps1.setString(3,f.getTitre());
            ps1.setString(4, f.getRealisateur());
            ps1.setString(5, f.getDuree());
            ps1.setString(6, f.getType());
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            
        }try {
                int executeUpdate = ps1.executeUpdate();
                  System.out.println("i"+executeUpdate);
            } catch (SQLException ex1) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
            }
     try{
		if(rs != null) rs.close();
		if (ps1 != null) ps1.close();
		if (c!= null) c.close();
	}catch(Exception e){
		e.printStackTrace();
	}
}

public static void modifier(Film f){
      try {
            c = conn.Connecter();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ps2 = c.prepareStatement("UPDATE \"Films\" SET \"titre\"=?,\"realisateur\"=?,\"duree\"=?,\"type\"=? WHERE \"numFilm\"=?");
            ps2.setInt(5, f.getNoFilm());

            ps2.setString(1,f.getTitre());
            ps2.setString(2, f.getRealisateur());
            ps2.setString(3, f.getDuree());
            ps2.setString(4, f.getType());
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
           
        }
 try {
                int executeUpdate = ps2.executeUpdate();
            } catch (SQLException ex1) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
            }
            try{
		if(rs != null) rs.close();
		if (ps1 != null) ps1.close();
		if (c!= null) c.close();
	}catch(Exception e){
		e.printStackTrace();
	}

    }

public static  ArrayList<Modele.Planning> inserePlanning(){

        try {
            c = conn.Connecter();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
      ArrayList listPlanning= new ArrayList();
        try {
            try {
                ps5 = c.prepareStatement("SELECT \"no_jour\",\"films\",\"heure_debut\",\"salle\" FROM \"Planning\"");

            } catch (SQLException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            rs2 = ps5.executeQuery();
           while(rs2.next()) {
                String date = rs2.getString(1);
                date = date.substring(0, 10);
                String string = rs2.getString(2);
                String string1 = rs2.getString(3);
                String string2 = rs2.getString(4);

               listPlanning.add(new Planning(date,string,string1,string2));

            }

        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
       try{
		if(rs0 != null) rs0.close();
		if (ps4 != null) ps4.close();
		if (c!= null) c.close();
	}catch(Exception e){
		e.printStackTrace();
	}


    return listPlanning;
}

 public static void supprimer(Film f){
       try {
            c = conn.Connecter();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ps3 = c.prepareStatement("DELETE FROM \"Films\" where \"numFilm\"=? ;");
            ps3.setInt(5, f.getNoFilm());
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
          
        }
         try {
                int executeUpdate = ps3.executeUpdate();
            } catch (SQLException ex1) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
            }
            try{
		if(rs != null) rs.close();
		if (ps1 != null) ps1.close();
		if (c!= null) c.close();
	}catch(Exception e){
		e.printStackTrace();
	}
    }

  public static int SelectNo(String s){
        try {
            c = conn.Connecter();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
       int aInt=-1;
        try {
            try {
                ps4 = c.prepareStatement("SELECT \"numFilm\" FROM \"Films\" WHERE \"titre\"=?;");
                ps4.setString(1, s);
            } catch (SQLException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            rs0 = ps4.executeQuery();
           while(rs0.next()) {
               aInt = rs0.getInt("numFilm");
            }

        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
       try{
		if(rs0 != null) rs0.close();
		if (ps4 != null) ps4.close();
		if (c!= null) c.close();
	}catch(Exception e){
		e.printStackTrace();
	}
        return aInt;
  }

  public static  ArrayList<String> insereDates(){

        try {
            c = conn.Connecter();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
      ArrayList<String> listDates= new ArrayList<String>();
        try {
            try {
                ps6 = c.prepareStatement("SELECT \"no_jour\" FROM \"Planning\"");

            } catch (SQLException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            rs3 = ps6.executeQuery();
           while(rs3.next()) {
                String date = rs3.getString(1);
                date = date.substring(0, 10);


               listDates.add(date);

            }

        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
       try{
		if(rs0 != null) rs0.close();
		if (ps4 != null) ps4.close();
		if (c!= null) c.close();
	}catch(Exception e){
		e.printStackTrace();
	}


    return listDates;
}
}

